/**
 * 调试工具函数
 */

import { parseJWT, isTokenExpired, isTokenNearExpiry, getTokenRemainingTime } from './tokenUtils'

/**
 * 打印当前token状态
 */
export function debugTokenStatus() {
  const token = localStorage.getItem('token')
  const refreshToken = localStorage.getItem('refreshToken')
  
  console.log('=== Token状态调试 ===')
  console.log('Access Token:', token ? `${token.substring(0, 20)}...` : 'null')
  console.log('Refresh Token:', refreshToken ? `${refreshToken.substring(0, 20)}...` : 'null')
  
  if (token) {
    const payload = parseJWT(token)
    console.log('Token Payload:', payload)
    console.log('Token已过期:', isTokenExpired(token))
    console.log('Token即将过期:', isTokenNearExpiry(token))
    console.log('Token剩余时间(秒):', getTokenRemainingTime(token))
    
    if (payload && payload.exp) {
      const expireTime = new Date(payload.exp * 1000)
      console.log('Token过期时间:', expireTime.toLocaleString())
    }
  }
  
  if (refreshToken) {
    const refreshPayload = parseJWT(refreshToken)
    console.log('Refresh Token Payload:', refreshPayload)
    console.log('Refresh Token已过期:', isTokenExpired(refreshToken))
    
    if (refreshPayload && refreshPayload.exp) {
      const expireTime = new Date(refreshPayload.exp * 1000)
      console.log('Refresh Token过期时间:', expireTime.toLocaleString())
    }
  }
  
  console.log('=== 调试结束 ===')
}

/**
 * 在控制台添加调试命令
 */
export function addDebugCommands() {
  if (typeof window !== 'undefined') {
    window.debugToken = debugTokenStatus
    window.clearTokens = () => {
      localStorage.removeItem('token')
      localStorage.removeItem('refreshToken')
      localStorage.removeItem('userInfo')
      console.log('已清除所有token')
    }
    console.log('调试命令已添加:')
    console.log('- window.debugToken() - 查看token状态')
    console.log('- window.clearTokens() - 清除所有token')
  }
}
